package com.ebelter.temperature.view;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;

import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebelter.temperature.R;


/**
 * Created by ZCQ on 2017/12/1
 * 组合控件1 不知道怎么起名字了 还是按序号来吧
 */

public class CombinView3 extends RelativeLayout{
    private RelativeLayout root;

    private TextView title1;
    private TextView title2;
    private TextView title3;
    private ImageView iv1;

    private String title1Str;
    private String title2Str;
    private String title3Str;
    private int station;//0减 1维持 2增加 -1不显示


    public CombinView3(Context context) {
        this(context,null);
    }

    public CombinView3(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CombinView3(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CombinView3);
        if (typedArray != null) {
            title1Str = typedArray.getString(R.styleable.CombinView3_title1);
            title2Str = typedArray.getString(R.styleable.CombinView3_title2);
            title3Str = typedArray.getString(R.styleable.CombinView3_title3);

            station = typedArray.getInt(R.styleable.CombinView3_station,-1);

            typedArray.recycle();
        }

        initView(getContext());
        initData();
    }

    private void initData() {
        title1.setText(title1Str);
        title2.setText(title2Str);
        title3.setText(title3Str);
        if (station==0){
            iv1.setImageResource(R.drawable.ic_lose);
            title3.setTextColor(Color.parseColor("#79e531"));
        }else if (station==1){
            iv1.setImageResource(R.drawable.ic_nochange);
            title3.setTextColor(Color.parseColor("#7f83dd"));
        }else if (station==2){
            iv1.setImageResource(R.drawable.ic_add);
            title3.setTextColor(Color.parseColor("#fdc605"));
        }else {
            iv1.setImageResource(0);
        }
    }


    private void initView(Context context) {
        View view =  View.inflate(context, R.layout.cb3_layout, this);//把那个布局当做是自己的孩子
        root = view.findViewById(R.id.cb2_root_rl);
        title1 = view.findViewById(R.id.title1_tv);
        title2 = view.findViewById(R.id.title2_tv);
        title3 = view.findViewById(R.id.title3_tv);
        iv1 = view.findViewById(R.id.cb3_iv1);

    }

    public void setTitle1Str(String title1Str,boolean refresh) {
        this.title1Str = title1Str;
        if (refresh) initData();
    }

    public void setTitle2Str(String title2Str,boolean refresh) {
        this.title2Str = title2Str;
        if (refresh) initData();
    }

    public void setTitle3Str(String title3Str,boolean refresh) {
        this.title3Str = title3Str;
        if (refresh) initData();
    }

    public void setStation(int station,boolean refresh) {
        this.station = station;
        if (refresh) initData();
    }
}
